﻿using System.Data.Linq;
using System.Linq;
using DbContent;
using Model;
using System.Collections.Generic;
using System;

namespace Helper
{   /// <summary>
    /// copyright:kklldog
    /// date:2011-08-29
    ///  qq:17014842
    /// </summary>
    public class DbHelper
    {
        private static DBFMDB db;
        /// <summary>
        /// 创建
        /// </summary>
        public static void CreateDb()
        {
           if(db==null)
            {
                db = new DBFMDB(DBFMDB._connStr);
            }
         //  db.DeleteDatabase();
           if (!db.DatabaseExists())
           {
               db.CreateDatabase();
           }
            
        }

        public static void AddSong(dbSong song)
        {
            dbSong s = new dbSong();
            s.url = song.url;
            s.title = song.title;
            s.picture = song.picture;
            s.artist = song.artist;
            s.albumtitle = song.albumtitle;
            s.channel = song.channel;
            db.Songs.InsertOnSubmit(s);
            db.SubmitChanges();
        }
        public static void AddFavSong(dbSong song)
        {
            dbSong s = new dbSong();
            s.url = song.url;
            s.title = song.title;
            s.picture = song.picture;
            s.artist = song.artist;
            s.albumtitle = song.albumtitle;
            s.channel = 0;
            db.Songs.InsertOnSubmit(s);
            db.SubmitChanges();
        }

        public static void DelFavSong(dbSong song)
        {
            dbSong oldS = db.Songs.Single(s => s.url == song.url&&s.channel==0);
            db.Songs.DeleteOnSubmit(oldS);
            db.SubmitChanges();
        }
        public static void DelSong(dbSong song)
        {
            if (song.channel==0)
            {
                return;
            }
            dbSong oldS = db.Songs.Single(s => s.url == song.url && s.channel == song.channel);
            db.Songs.DeleteOnSubmit(oldS);
            db.SubmitChanges();
        }
        public static List<dbSong> GetFavSongList()
        {
            var list = db.Songs.Where(s=>s.channel==0).ToList();
           // return RandomSong(list);
            return list;
        }

        private static List<dbSong> RandomSong(List<dbSong> list)
        {
            List<dbSong> tempList = new List<dbSong>();
            Random rad = new Random();
            while (list.Count > 0)
            {
                int r = rad.Next(0, list.Count);
                var song = list[r];
                tempList.Add(song);
                list.Remove(song);
            }
            return tempList;
        }

        public static bool IsFavSong(dbSong song)
        {
            if (song == null)
                return false;
            var obj = db.Songs.Where(o => o.channel==0&&o.url == song.url);
            if (obj != null&&obj.Count()>0)
                return true;
            else
                return false;
        }

        public static List<dbSong> GetListByChannel(ChannelType t)
        {
            var list= db.Songs.Where(s=>s.channel==(int)t).ToList();
            //return RandomSong(list);
            return list;
        }

        public static void DelSongNotFav()
        {
            var oldS = db.Songs.Where(s => s.channel!=0);
            foreach (var item in oldS)
            {
                db.Songs.DeleteOnSubmit(item);
                
            }
            db.SubmitChanges();
        }

        public static void DelSongByChannel(ChannelType t)
        {
            var oldS = db.Songs.Where(s => s.channel == (int)t);
            db.Songs.DeleteAllOnSubmit(oldS);
            db.SubmitChanges();
        }
    }
}
